package com.lzq.system.web;

import com.lzq.api.code.SystemCode;
import com.lzq.api.commons.ResponseResult;
import com.lzq.api.commons.ResponseResultFactory;
import com.lzq.api.commons.SystemUtils;
import com.lzq.system.entity.UserEntity;
import com.lzq.system.service.UserService;
import javafx.geometry.Pos;
import net.bytebuddy.pool.TypePool;
import org.apache.catalina.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import sun.invoke.empty.Empty;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    //我在哪个类里面打印的日志
    final Logger logger = LoggerFactory.getLogger(UserController.class);

    final int UPD_USER_ZERO = 0;
    final int MAX_ACCOUNT = 6;
    final int MAX_PASS = 13;

    @Autowired
    UserService userService;

    /**
     * 添加用户
     * @param userEntity
     * @return 是否成功
     */
    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public ResponseResult addUser(UserEntity userEntity) {

        logger.info("system user addUser start ");

        //判断前端传来的值
        //参数为空
        if (SystemUtils.isNull(userEntity)) {
            logger.error("system user addUser UserEntity is NULL");
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_PARAM_NULL);
            logger.info("system user addUser return msg:" + responseResult);
            return responseResult;
        }

        //用户名为空
        if(SystemUtils.isNUllOrEmpty(userEntity.getUname())) {
            logger.error("system user addUser UName is NULL");
            logger.info("param:" + userEntity);
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_UNAME_NULL);
            logger.info("system user addUser return msg:" + responseResult);
            return responseResult;
        }

        //账号为空
        if(SystemUtils.isNUllOrEmpty(userEntity.getUaccount())) {
            logger.error("system user addUser UACCOUNT is NULL");
            logger.info("param:" + userEntity);
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_UACCOUNT_NULL);
            logger.info("system user addUser return msg:" + responseResult);
            return responseResult;
        }

        //密码为空
        if(SystemUtils.isNUllOrEmpty(userEntity.getUpass())) {
            logger.error("system user addUser UPASS is NULL");
            logger.info("param:" + userEntity);
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_UPASS_NULL);
            logger.info("system user addUser return msg:" + responseResult);
            return responseResult;
        }

        //手机为空
        if(SystemUtils.isNUllOrEmpty(userEntity.getUpass())) {
            logger.error("system user addUser UPHONE is NULL");
            logger.info("param:" + userEntity);
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_UPHONE_NULL);
            logger.info("system user addUser return msg:" + responseResult);
            return responseResult;
        }

        //账号长度不能大于6
        if(userEntity.getUaccount().trim().length() > MAX_ACCOUNT) {
            logger.error("system user addUser UACCOUNT > 6");
            logger.info("param:" + userEntity);
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_UACCOUNT_SIZE);
            logger.info("system user addUser return msg:" + responseResult);
            return responseResult;
        }

        //密码长度不能大于13
        if(userEntity.getUpass().trim().length() > MAX_PASS) {
            logger.error("system user addUser UPASS > 13");
            logger.info("param:" + userEntity);
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_UPASS_SIZE);
            logger.info("system user addUser return msg:" + responseResult);
            return responseResult;
        }

        //千万不要把密码记录成明文
        /**
         * 密码的加密
         * 调用DigestUtils中的哈希方法md5DigestAsHex
         * 哈希方法需要先让对象变成一个二进制数组getBytes（）
         */
        logger.info("system user addUser UPASS digest");
        userEntity.setUpass(DigestUtils.md5DigestAsHex(userEntity.getUpass().getBytes()));

        //判断完成保存用户信息
        //调用service方法
        logger.info("system user addUser userService addUser start");
        boolean result = userService.addUser(userEntity);
        logger.info("system user addUser userService addUser end:" + result);


        //根据保存结果返回给前台信息
        return getResponseResult(result,"addUser");
    }


    /**
     * 删除用户
     * @param uid = "1"删除一个；= "1,2,3,4" 删除多个
     * @return 是否成功
     */
    @RequestMapping(value = "/delUser",method = RequestMethod.POST)
    public ResponseResult delUser(String uid) {

        logger.info("system user delUser start");

        //判断传过来的参数是否为空
        if (SystemUtils.isNUllOrEmpty(uid)) {
            logger.error("system user delUser uid is NULL");
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_DEL_FAIL_Uid_NULL);
            logger.info("system user delUser return msg:" + responseResult);
            return responseResult;
        }

        //简单的逻辑判断；在这判断或者在业务层判断都可以
        logger.info("system user delUser UserService start");
        boolean bl = userService.delUser(uid);
        logger.info("system user delUser UserService end result:" + bl);

        return getResponseResult(bl,"delUser");
    }


    /**
     * 修改用户数据
     * 根据传过来的userEntity中的uid判断要修改的用户，如果userEntity中其他数据不为空则修改，为空则不修改
     * @param userEntity
     * @return 是否成功
     */
    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    public ResponseResult updateUser(UserEntity userEntity) {
        logger.info("system user updateUser start");

        //判断传来的对象是否为空
        if(SystemUtils.isNull(userEntity)) {
            logger.error("system user updateUser UserEntity is Null");
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.getSYSTEM_USER_ERROR_UPD_FAIL_USERENTITY_NULL);
            logger.info("system user updateUser return msg : "+responseResult);
            return responseResult;
        }

        //判断传来的uid是否为空
        if(SystemUtils.isNull(userEntity.getUid()) || userEntity.getUid() == UPD_USER_ZERO) {
            logger.error("system user updateUser Uid is Null");
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.getSYSTEM_USER_ERROR_UPD_FAIL_UID_NULL);
            logger.info("system user updateUser return msg : "+responseResult);
            return responseResult;
        }

        //在service层写逻辑
        logger.info("system user updateUser UserService start");
        boolean bl = userService.updateUser(userEntity);
        logger.info("system user updateUser UserService start result:" + bl);

        return getResponseResult(bl,"updateUser");
    }


    /**
     *查询所有用户
     * @return 所有用户
     */
    @RequestMapping(value = "/findallUser",method = RequestMethod.GET)
    public ResponseResult<List<UserEntity>> findallUser() {
        logger.info("system user findallUser start");

        logger.info("system user findallUser userService start");
        List<UserEntity> userEntities = userService.findallUser();
        logger.info("system user findallUser userService end list<userEntity>:" + userEntities);

        return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_SYSTEM_SUCCESS, userEntities);
    }

    /**
     * 条件查询
     * @param userEntity
     * @return 查询到的用户
     */
    @RequestMapping(value = "/findAllUserByWhere",method = RequestMethod.POST)
    public ResponseResult<List<UserEntity>> findAllUserByWhere(UserEntity userEntity) {
        logger.info("system user findAllUserByWhere start");

        logger.info("system user findAllUserByWhere userService start");
        List<UserEntity> allUserByWhere = userService.findAllUserByWhere(userEntity);
        logger.info("system user findAllUserByWhere userService end list<userEntity>:" + allUserByWhere);

        return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_SYSTEM_SUCCESS, allUserByWhere);
    }

    /**
     * 根据时间查询
     * @param userEntity
     * @return
     */
    @RequestMapping(value = "/findUserByTime",method = RequestMethod.POST)
    public ResponseResult<List<UserEntity>> findUserByTime(UserEntity userEntity) {
        logger.info("system user findUserByTime    start");

        logger.info("system user findUserByTime    userService start");
        List<UserEntity> allUserByTime = userService.findUserByTime(userEntity.getStartTime(),userEntity.getEndTime());
        logger.info("system user findUserByTime    userService end list<userEntity>:" + allUserByTime);

        return ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_SYSTEM_SUCCESS, allUserByTime);
    }



    //判断返回结果
    public ResponseResult getResponseResult(boolean bl,String MethodName) {
        ResponseResult responseResult;
        if(bl) {
            logger.info("system user"+ MethodName +" success");
            responseResult = new ResponseResult(SystemCode.TRAFFIC_SYSTEM_SUCCESS);
        } else {
            logger.error("system user"+ MethodName +" fail");
            responseResult = new ResponseResult(SystemCode.TRAFFIC_SYSTEM_ERROR);
        }
        return responseResult;
    }
}
